Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix expression labels in guide_coloursteps() and guide_bins() #6007

Merged
merged 11 commits into from
Dec 2, 2024

Conversation

teunbrand
Copy link
Collaborator

This PR aims to fix #6005.

Briefly, internally we dance around <expression> labels by casting them to lists of expressions when storing them in <data.frame>s and later unpacking such lists to expressions when handing them off to {grid}. I forgot to do this for guide_coloursteps() and guide_bins(), which have very custom code for getting the keys from the scale. This PR rectifies that.

Reprex from the issue:

devtools::load_all("~/packages/ggplot2")
#> ℹ Loading ggplot2

p <- data.frame(x = 1:5, y = 1:5) |>
  ggplot(aes(x = x, y = y, color = y))+
  geom_text(label = paste0("x^", 1:5), parse = TRUE)+
  scale_color_stepsn(
    breaks = 1:5,
    colors = c("red", "orange", "yellow","green", "blue"),
    labels = parse(text = paste0("x^", 1:5))
  )
p

p + guides(colour = "bins")

Created on 2024-07-18 with reprex v2.1.1

Comment on lines 118 to 120
if (is.expression(labels)) {
labels <- as.list(labels)
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if it wouldn't be better if this was the responsibility of Scale$get_labels().

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah... Do we have this call in other places so that moving it to Scale$get_labels() will allow us to remove some code elsewhere?

Copy link
Collaborator Author

@teunbrand teunbrand Aug 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In some places for sure. The issue is that, at the moment, it is a balanced trade-off. We have 3 guides where this guard is used, but we also have 3 Scale$get_labels() methods that might generate expressions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that it logically belongs in get_labels() as it ensures the output of that function is usable. It shouldn't be the responsibility of the caller to further process the return value to make it work

Copy link
Member

@thomasp85 thomasp85 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment on lines 114 to 115
fmt <- if (even.steps) seq_along else identity
key$.value <- fmt(breaks)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really better?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll revert this if you find the former more readable

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I do

@thomasp85
Copy link
Member

I see you take my views on git commit messages serious :-)

@teunbrand teunbrand merged commit f13d9ab into tidyverse:main Dec 2, 2024
13 checks passed
@teunbrand teunbrand deleted the bin_guide_expression_labels branch December 2, 2024 10:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

scale_color_stepsn() will not accept expression vector for scale labels
2 participants